<template>
  <div v-if="isOpen" class="modal-overlay" @click.self="onClose">
    <div class="modal-container" :class="{ 'modal-enter': isOpen, 'modal-leave': !isOpen }">
      <div class="modal-header">
        <h2 class="modal-title">创建账号</h2>
        <button class="close-btn" @click="onClose" aria-label="关闭">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
            <path d="M18 6L6 18" stroke="var(--gold-primary)" stroke-width="2" stroke-linecap="round" />
            <path d="M6 6L18 18" stroke="var(--gold-primary)" stroke-width="2" stroke-linecap="round" />
          </svg>
        </button>
      </div>
      <div class="modal-body">
        <form @submit.prevent="handleSubmit" class="register-form">
          <!-- 账号输入 -->
          <div class="form-group">
            <label for="username" class="form-label">账号</label>
            <input
              id="username"
              v-model="form.username"
              type="text"
              placeholder="请设置账号名，6-20位字母、数字"
              required
              class="form-input"
            />
          </div>
          
          <!-- 密码输入 -->
          <div class="form-group">
            <label for="password" class="form-label">密码</label>
            <input
              id="password"
              v-model="form.password"
              type="password"
              placeholder="请设置密码，8-20位字母、数字和符号"
              required
              class="form-input"
            />
          </div>
          
          <!-- 确认密码 -->
          <div class="form-group">
            <label for="confirmPassword" class="form-label">确认密码</label>
            <input
              id="confirmPassword"
              v-model="form.confirmPassword"
              type="password"
              placeholder="请再次输入密码"
              required
              class="form-input"
            />
          </div>
          
          <!-- 手机号输入 -->
          <div class="form-group">
            <label for="phone" class="form-label">手机号</label>
            <div class="phone-input-group">
              <input
                id="phone"
                v-model="form.phone"
                type="tel"
                placeholder="请输入手机号"
                required
                class="form-input phone-input"
              />
              <button type="button" class="get-code-btn" @click="getVerificationCode" :disabled="isGettingCode">
                {{ isGettingCode ? `${countdown}s` : '获取验证码' }}
              </button>
            </div>
          </div>
          
          <!-- 验证码输入 -->
          <div class="form-group">
            <label for="verificationCode" class="form-label">验证码</label>
            <input
              id="verificationCode"
              v-model="form.verificationCode"
              type="text"
              placeholder="请输入验证码"
              required
              class="form-input"
            />
          </div>
          
          <!-- 用户协议 -->
          <div class="agreement-group">
            <label class="agreement-label">
              <input type="checkbox" v-model="form.agreement" required class="checkbox" />
              <span>
                我已阅读并同意
                <a href="#" class="agreement-link">《用户协议》</a>
                和
                <a href="#" class="agreement-link">《隐私政策》</a>
              </span>
            </label>
          </div>
          
          <!-- 错误/成功信息 -->
          <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
          <div v-if="successMessage" class="success-message">{{ successMessage }}</div>
          
          <!-- 注册按钮 -->
          <button type="submit" class="register-btn">注册</button>
          
          <!-- 登录链接 -->
          <div class="login-link">
            已有账号？
            <button type="button" class="login-button" @click="goToLogin">立即登录</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'

// Props
const props = defineProps({
  isOpen: {
    type: Boolean,
    default: false
  }
})

// Emits
const emit = defineEmits(['close', 'goToLogin'])

// 表单数据
const form = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  phone: '',
  verificationCode: '',
  agreement: false
})

const errorMessage = ref('')
const successMessage = ref('')
const isGettingCode = ref(false)
const countdown = ref(60)
let countdownTimer = null

// 处理关闭事件
const onClose = () => {
  emit('close')
  resetForm()
}

// 处理表单提交
const handleSubmit = () => {
  // 表单验证
  if (!form.username || form.username.length < 6 || form.username.length > 20) {
    errorMessage.value = '账号名需为6-20位字母或数字'
    return
  }
  
  if (!form.password || form.password.length < 8 || form.password.length > 20) {
    errorMessage.value = '密码需为8-20位字母、数字和符号'
    return
  }
  
  if (form.password !== form.confirmPassword) {
    errorMessage.value = '两次输入的密码不一致'
    return
  }
  
  if (!form.phone || !/^1[3-9]\d{9}$/.test(form.phone)) {
    errorMessage.value = '请输入有效的手机号码'
    return
  }
  
  if (!form.verificationCode || form.verificationCode.length !== 6) {
    errorMessage.value = '请输入6位验证码'
    return
  }
  
  if (!form.agreement) {
    errorMessage.value = '请阅读并同意用户协议和隐私政策'
    return
  }
  
  // 模拟注册请求
  console.log('注册信息:', form)
  
  // 模拟注册成功
  errorMessage.value = ''
  successMessage.value = '注册成功！正在跳转...'
  
  // 注册成功后跳转
  setTimeout(() => {
    emit('close')
    resetForm()
    emit('goToLogin') // 跳转到登录
  }, 1500)
}

// 获取验证码
const getVerificationCode = () => {
  if (!form.phone || !/^1[3-9]\d{9}$/.test(form.phone)) {
    errorMessage.value = '请输入有效的手机号码'
    return
  }
  
  // 模拟发送验证码
  isGettingCode.value = true
  countdown.value = 60
  
  // 清除之前的计时器
  if (countdownTimer) {
    clearInterval(countdownTimer)
  }
  
  // 开始倒计时
  countdownTimer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(countdownTimer)
      isGettingCode.value = false
    }
  }, 1000)
  
  console.log('发送验证码到:', form.phone)
}

// 跳转到登录
const goToLogin = () => {
  emit('close')
  emit('goToLogin')
}

// 重置表单
const resetForm = () => {
  Object.keys(form).forEach(key => {
    form[key] = ''
  })
  form.agreement = false
  errorMessage.value = ''
  successMessage.value = ''
  
  // 清除倒计时
  if (countdownTimer) {
    clearInterval(countdownTimer)
    countdownTimer = null
    isGettingCode.value = false
    countdown.value = 60
  }
}
</script>

<style scoped>
/* 全局变量 - 从应用中继承或定义 */
:root {
  --gold-primary: #d4af37;
  --gold-secondary: #f0e68c;
  --gold-tertiary: #ffd700;
  --border-color: rgba(212, 175, 55, 0.3);
  --bg-primary: #0d0d0d;
  --bg-secondary: #1a1a1a;
  --bg-modal: rgba(0, 0, 0, 0.95);
  --text-primary: #ffffff;
  --text-secondary: #cccccc;
  --success-color: #4caf50;
}

/* 遮罩层 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  animation: fadeIn 0.3s ease;
}

/* 弹窗容器 */
.modal-container {
  background: var(--bg-modal);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 10px 40px rgba(212, 175, 55, 0.3), 0 0 60px rgba(212, 175, 55, 0.1) inset;
  animation: slideIn 0.4s ease;
  position: relative;
}

/* 弹窗头部 */
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border-color);
}

.modal-title {
  font-size: 24px;
  font-weight: bold;
  color: var(--gold-primary);
  text-shadow: 0 0 10px rgba(212, 175, 55, 0.5);
}

.close-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 5px;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background: rgba(212, 175, 55, 0.1);
  transform: rotate(90deg);
}

/* 弹窗内容 */
.modal-body {
  padding: 24px;
}

/* 表单样式 */
.register-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-label {
  font-size: 14px;
  color: var(--text-secondary);
}

.form-input {
  padding: 12px 16px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 16px;
  transition: all 0.3s ease;
}

.form-input:focus {
  outline: none;
  border-color: var(--gold-primary);
  box-shadow: 0 0 0 3px rgba(212, 175, 55, 0.2);
  background: rgba(255, 255, 255, 0.15);
}

.form-input::placeholder {
  color: rgba(255, 255, 255, 0.4);
}

/* 手机号输入组 */
.phone-input-group {
  display: flex;
  gap: 12px;
  align-items: center;
}

.phone-input {
  flex: 1;
}

.get-code-btn {
  padding: 12px 20px;
  background: rgba(212, 175, 55, 0.2);
  border: 1px solid var(--gold-primary);
  border-radius: 6px;
  color: var(--gold-primary);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
}

.get-code-btn:hover:not(:disabled) {
  background: rgba(212, 175, 55, 0.3);
  box-shadow: 0 0 10px rgba(212, 175, 55, 0.4);
}

.get-code-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(212, 175, 55, 0.5);
}

/* 用户协议 */
.agreement-group {
  font-size: 14px;
  color: var(--text-secondary);
}

.agreement-label {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  cursor: pointer;
  line-height: 1.5;
}

.checkbox {
  accent-color: var(--gold-primary);
  cursor: pointer;
  margin-top: 2px;
}

.agreement-link {
  color: var(--gold-primary);
  text-decoration: none;
  transition: color 0.3s ease;
}

.agreement-link:hover {
  color: var(--gold-secondary);
  text-shadow: 0 0 5px rgba(212, 175, 55, 0.5);
}

/* 错误/成功信息 */
.error-message {
  background: rgba(255, 0, 0, 0.2);
  border: 1px solid rgba(255, 0, 0, 0.3);
  border-radius: 6px;
  color: #ff6b6b;
  padding: 10px;
  text-align: center;
  font-size: 14px;
}

.success-message {
  background: rgba(76, 175, 80, 0.2);
  border: 1px solid rgba(76, 175, 80, 0.3);
  border-radius: 6px;
  color: var(--success-color);
  padding: 10px;
  text-align: center;
  font-size: 14px;
}

/* 注册按钮 */
.register-btn {
  padding: 14px 20px;
  background: linear-gradient(135deg, var(--gold-primary), var(--gold-tertiary));
  border: none;
  border-radius: 6px;
  color: #000;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(212, 175, 55, 0.4);
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(212, 175, 55, 0.6);
  background: linear-gradient(135deg, var(--gold-secondary), var(--gold-tertiary));
}

.register-btn:active {
  transform: translateY(0);
}

/* 登录链接 */
.login-link {
  text-align: center;
  margin-top: 10px;
  color: var(--text-secondary);
  font-size: 14px;
}

.login-button {
  background: none;
  border: none;
  color: var(--gold-primary);
  cursor: pointer;
  font-size: 14px;
  transition: color 0.3s ease;
}

.login-button:hover {
  color: var(--gold-secondary);
  text-shadow: 0 0 5px rgba(212, 175, 55, 0.5);
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .modal-container {
    margin: 20px;
    width: calc(100% - 40px);
  }
  
  .modal-header,
  .modal-body {
    padding: 16px;
  }
  
  .modal-title {
    font-size: 20px;
  }
  
  .phone-input-group {
    flex-direction: column;
  }
  
  .phone-input {
    width: 100%;
  }
  
  .get-code-btn {
    width: 100%;
  }
  
  .agreement-label {
    flex-direction: column;
  }
}
</style>